package core.proto.vis.predicate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JList;

import prefuse.data.Tuple;
import prefuse.data.expression.AbstractPredicate;
import prefuse.data.tuple.TupleSet;
import prefuse.visual.EdgeItem;

/**
 * Predicate that can be used to determine what color to make edges.
 * If there is a selected word in the list of words and the list is visible, 
 * only color the edges that contains that word. Else, color edges that belong 
 * to the given group.  
 */
public class EdgeMatchesSelectedWordOrInFocusGroup extends AbstractPredicate {

	JList list; 
	TupleSet group;
	Map<String, List<EdgeItem>> attrMapping;
	
	/**
	 * @param list The list of keywords on the currently active node. 
	 * @param group The group used to determine whether to include a tuple if there's no selected word in the JList. 
	 * @param attrMapping Map of keyword->edges containing keyword, for the currently focused node in the viz. 
	 */
	public EdgeMatchesSelectedWordOrInFocusGroup (JList list, TupleSet group, Map<String, List<EdgeItem>> attrMapping) {
		this.list = list;
		this.group = group;
		this.attrMapping = attrMapping;
	}

	public boolean getBoolean (Tuple tuple) {
		if (list.isVisible() && ! list.isSelectionEmpty()) {
			if (tuple instanceof EdgeItem) {
				String attr = list.getSelectedValue().toString();
				if (attrMapping.containsKey(attr)){
					return attrMapping.get(attr).contains(tuple);
				}
			}
			return false;
		} else {
			return group.containsTuple(tuple);
		}
	}
}
